package Offer.offer2016;

public class Test14
{
	public static void main(String[] args) 
	{
		int[] array={1,2,3,4,5,6,7,8};
		for(int k=0;k<array.length;k++)
		{
			System.out.print(array[k]);
		}	
		System.out.println();
		//reOrderArray(array);
		exchangeOddEven.exchangePosition(array);
		for(int k=0;k<array.length;k++)
		{
			System.out.print(array[k]);
		}	

	}
    public static void reOrderArray(int[] array)
    {
    	if(array==null||array.length<=1)
    		return;
        int i = 0;
        int j = array.length-1;
        while(i<j)
        {
        	   while(i<j&&(array[i]&0x1)!=0)
               {
               	i++;
               }
               while(i<j&&(array[j]&0x1)==0)
               {
               	j--;
               }
               if(i<j)
               {
            	   int temp=array[i];
                   array[i]=array[j];
                   array[j]=temp;
               }              
        }     
    }
}
class exchangeOddEven{
	public static void exchangePosition(int []a){
		if(a == null || a.length <= 0){
			return ;
		}
		int begin = 0; 
		int end = a.length - 1;
		while(begin < end){
			while(begin < end && (a[begin] & 0x1) == 1){
				begin ++;
			}
			while(begin < end && (a[end] & 0x1) == 0){
				end --;
			}
			int tmp = a[begin];
			a[begin] = a[end];
			a[end] = tmp;
		}
	}
}
